package listings;

import classes.AdminDB;
import classes.BaseServlet;
import classes.comment;
import classes.ObjetoDB;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alvaro
 */
public class listComments extends BaseServlet {

    RequestDispatcher rd;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException
        {

            //Creacion de la sesion donde se meteran los comentarios
            HttpSession s = request.getSession();
            
            //Parametros obtenidos del formulario
            String in = request.getParameter("comments");//Para ver a donde redirigimos
            String product = request.getParameter("product");
        
            
            
            
            
            AdminDB adb = new AdminDB();
            comment com = new comment();
            
            //Lista para los comentarios
            List<comment> comentarios = null;

            //Recuperamos los comentarios
            if(product.equals("index"))
            {
                System.out.println("Mostramos los comentarios del Index!");
                comentarios = com.getAllComentarios(adb.getConnection(),"index");
              
                System.out.println("Hemos llegado al server para buscar por: "+product);
                s.setAttribute("listaDecomentarios", comentarios);
            }    

 
         
            //Vemos a donde redirigimos:
            //A la pagina donde vemos todos los comentarios
            if(in.equals("comments")){
                System.out.println("Redirigimos para ver los comentarios");
                response.sendRedirect("commentsView.jsp");
            }
            //Al index ya que el comentario se ha hecho sobre algun producto
            else//es igual a index
            {
                
                System.out.println("Redirigimos a otro sitio que no es el listado de comentarios");
                response.sendRedirect("index.jsp");
            }
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listComments.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listComments.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(listComments.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(listComments.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
